# 蓝色框为桥梁混凝土和钢筋的参数，由具体桥梁决定，此处为假设值。程序设置时，我们需手动输入。
# 黄色框为通过计算所得的。
# 绿色框为固定值。
# 橘色框为计算机裂缝识别所得参数。
from userModel import useModel

# 基于斜裂缝信息的钢筋混凝土简支梁受剪状态评估

import math

alpha1 = 1
alpha2 = 0.7


def oblique(S, fyVs, Es1, ks, Asvs, b, h, As, fc, ft, Ec, fy, Es2, theta):
    # 握筋间距S（mm）
    # 箍筋屈服强度fyVs(Mpa)
    # 箍筋弹性模量Es(Mpa) - Es1
    # 钢筋合力作用点到边缘的距离δs（mm）
    # 箍筋截面积Asvs(mm^2)
    # 宽度b（mm）
    # 高度h(mm)
    # 钢筋截面积As(mm^2)
    # 混凝土抗压强度fc (Mpa)
    # 混凝土抗拉强度ft(Mpa)
    # 混凝土弹性模量Ec(Mpa)
    # 钢筋屈服fy(Mpa)
    # 钢筋弹性模量Es(Mpa) - Es2
    # 混凝土斜裂缝角度θ(输入弧度制)

    # theta = theta*math.pi/180

    try:
        h0 = h - ks

        # 应变ε1（用变形量除以原长计算）
        eps1 = 0.00008

        # 内力臂长度jd
        jd = 0.9 * h0

        # 混凝土开裂应变εcr
        eps_cr = ft / Ec

        n = Es2 / Ec
        p = As / (b * h0)

        x = (2 * n * p * pow(h0, 2) + pow(h, 2)) / (2 * n * p * h0 + 2 * h)

        # 截面开裂弯矩Mcr(KN*m)
        Mcr = 2 * Es2 / Ec * ft * As * ((h0 - x) * (h0 - x / 3) / (h - x)) + ft * b * (h - x) * (h / 2 + x / 6)
        # 截面开裂弯矩Mcr(KN*m)
        Mu = abs(fy * As * (h0 - 0.197 * x) - 0.1714 * fc * b * pow(x, 2))

        f1 = alpha1 * alpha2 * ft / (1 + pow(500 * eps1, 0.5))
        f01 = alpha1 * alpha2 * ft / (1 + pow(500 * eps_cr, 0.5))

        tanthe = math.tan(theta)
        v1 = f1 / (tanthe + 1 / tanthe) + 1 / (tanthe + 1 / tanthe) * (
                    Asvs * fyVs / (S * b) + f1 * (pow(math.cos(theta), 2))) / (pow(math.sin(theta), 2))
        v0 = f01 / (tanthe + 1 / tanthe) + 1 / (tanthe + 1 / tanthe) * (
                    Asvs * fyVs / (S * b) + f01 * (pow(math.cos(theta), 2))) / (pow(math.sin(theta), 2))

        v = v1 * b * jd
        f2 = v1 * (tanthe + 1 / tanthe) - f1
        f02 = v0 * (tanthe + 1 / tanthe) - f1
        Vu = v0 * b * jd
        return jd, eps_cr, h0, n, p, x, Mcr, Mu, f1, f01, v1, v0, v, f2, f02, Vu
    except:
        return ["错误的参数值\n"]


def outputOblique(S=100, fyVs=235, Es1=200000, ks=30, Asvs=2906.604147, b=15000, h=2000, As=58890, fc=32.4, ft=2.65,
                  Ec=34500, fy=235, Es2=200000):
    positive, negative = useModel(1, "predict/")
    result = ""
    for i in negative:
        # print("Negative: " + i)
        result += "Negative: " + i + "\n"
    for i in positive:
        # print("-"*50)
        result += "-" * 50 + "\n"
        # print(i[0] + " 面积:" + str(i[1]) + " 长度:" + str(i[2]) + " 宽度:" + str(i[3]) + " 斜度(弧度制):" + str(i[4]))
        result += i[0] + " 面积:" + str(i[1]) + " 长度:" + str(i[2]) + " 宽度:" + str(i[3]) + " 斜度(弧度制):" + str(i[4]) + "\n"
        # print("-"*50)
        result += "-" * 50 + "\n"
    for i in positive:
        angle = i[4]
        # print("演示数据为：%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f"%(S,fyVs,Es1,ks,Asvs,b,h,As,fc,ft,Ec,fy,Es2,angle,angle))
        result += "演示数据为：%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f" % (
        S, fyVs, Es1, ks, Asvs, b, h, As, fc, ft, Ec, fy, Es2, angle, angle) + "\n"
        oblique_eg = oblique(S, fyVs, Es1, ks, Asvs, b, h, As, fc, ft, Ec, fy, Es2, angle)
        if len(oblique_eg) == 1:
            return oblique_eg[0]

        v = oblique_eg[12]
        Vu = oblique_eg[15]

        # print("-"*50)
        result += "-" * 50 + "\n"
        # print("评估结果：")
        result += "评估结果：" + "\n"
        if (v > Vu):
            # print("截面剪力大于抗剪承载力,破坏.")
            result += "截面剪力大于抗剪承载力,破坏." + "\n"
        else:
            # print("截面剪力不大于抗剪承载力,未破坏.")
            result += "截面剪力不大于抗剪承载力,未破坏." + "\n"
        # print("-"*50)
        result += "-" * 50 + "\n\n"
        return result
